#include <QtCore/QString>
#include <QtTest/QSignalSpy>
#include <QtTest/QtTest>

#include "../../Core/case.h"
#include "../../Core/plateau.h"

class TestsJeu : public QObject {
    Q_OBJECT

public:
    TestsJeu();

private Q_SLOTS:
	void testCaseCtor();
	void testCaseNumJoueur();
	void testCaseNumJoueur_data();
	void testCaseModif();
	void testEgalitePlateau();
	void testOpCopPlateau();
	void testSimuler();
	void testSimuler_data();
};

TestsJeu::TestsJeu() {
}

void TestsJeu::testCaseCtor() {
	Case c(5);
	QCOMPARE(c.numero(), 5);
	QCOMPARE(c.nbGraines(), 4);
}

void TestsJeu::testCaseNumJoueur() {
	QFETCH(int, numCase);
	QFETCH(int, numJoueur);
	Case c(numCase);
	QCOMPARE(c.numJoueur(), numJoueur);
}

void TestsJeu::testCaseNumJoueur_data() {
	QTest::addColumn<int>("numCase");
	QTest::addColumn<int>("numJoueur");
	QTest::newRow("Case 5") << 5 << 1;
	QTest::newRow("Case 6") << 6 << 2;
}

void TestsJeu::testCaseModif() {
	Case c(5);
	//QSignalSpy sp(&c, SIGNAL(sig_nbGrainesModifie(int,int)));
	c.setNbGraines(2);
	//QCOMPARE(sp.count(), 1);
	/*QList<QVariant> l = sp.takeFirst();
	QCOMPARE(l.at(0).toInt(), c.numero());
	QCOMPARE(l.at(1).toInt(), 2);*/
	QCOMPARE(c.nbGraines(), 2);
}

void TestsJeu::testEgalitePlateau() {
	QVERIFY2(Plateau() == Plateau(), "Plateaux vides");
	Plateau p;
	p.m_cases[0]->setNbGraines(0);
	p.m_cases[2]->setNbGraines(2);
	p.m_cases[11]->setNbGraines(11);
	QVERIFY2(p == p, "Plateaux modifiés");
}

void TestsJeu::testOpCopPlateau() {
	Plateau p;
	p.m_cases[0]->setNbGraines(0);
	p.m_cases[2]->setNbGraines(2);
	p.m_cases[11]->setNbGraines(11);
	Plateau p2;
	p2 = p;
	QCOMPARE(p, p2);
}

void TestsJeu::testSimuler() {
	Plateau final;
	QFETCH(Plateau, plateau);
	QFETCH(int, res);
	QFETCH(int, pos);
	QFETCH(Plateau, plateau_final);
	QCOMPARE(plateau.simulerCoup(pos, final), res);
	QCOMPARE(final, plateau_final);
}

void TestsJeu::testSimuler_data() {
	QTest::addColumn<Plateau>("plateau");
	QTest::addColumn<int>("res");
	QTest::addColumn<int>("pos");
	QTest::addColumn<Plateau>("plateau_final");
	Plateau p;
	Plateau pf;
	p.m_cases[5]->setNbGraines(2);
	p.m_cases[6]->setNbGraines(2);
	p.m_cases[7]->setNbGraines(2);
	pf.m_cases[5]->setNbGraines(0);
	pf.m_cases[6]->setNbGraines(0);
	pf.m_cases[7]->setNbGraines(0);
	QTest::newRow("5:2 6:2 7:2") << p << 6 << 5 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[11]->setNbGraines(2);
	p.m_cases[0]->setNbGraines(2);
	p.m_cases[1]->setNbGraines(2);
	pf.m_cases[11]->setNbGraines(0);
	pf.m_cases[0]->setNbGraines(0);
	pf.m_cases[1]->setNbGraines(0);
	QTest::newRow("0:2 1:2 11:2") << p << 6 << 11 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[5]->setNbGraines(1);
	p.m_cases[6]->setNbGraines(2);
	pf.m_cases[5]->setNbGraines(0);
	pf.m_cases[6]->setNbGraines(0);
	QTest::newRow("5:1 6:2") << p << 3 << 5 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[11]->setNbGraines(1);
	p.m_cases[0]->setNbGraines(2);
	pf.m_cases[11]->setNbGraines(0);
	pf.m_cases[0]->setNbGraines(0);
	QTest::newRow("0:2 1:2") << p << 3 << 11 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(0);
	p.m_cases[1]->setNbGraines(10);
	p.m_cases[6]->setNbGraines(2);
	p.m_cases[7]->setNbGraines(1);
	p.m_cases[8]->setNbGraines(2);
	p.m_cases[9]->setNbGraines(1);
	p.m_cases[10]->setNbGraines(2);
	p.m_cases[11]->setNbGraines(1);
	pf.m_cases[0]->setNbGraines(0);
	pf.m_cases[1]->setNbGraines(0);
	pf.m_cases[2]->setNbGraines(5);
	pf.m_cases[3]->setNbGraines(5);
	pf.m_cases[4]->setNbGraines(5);
	pf.m_cases[5]->setNbGraines(5);
	pf.m_cases[6]->setNbGraines(0);
	pf.m_cases[7]->setNbGraines(0);
	pf.m_cases[8]->setNbGraines(0);
	pf.m_cases[9]->setNbGraines(0);
	pf.m_cases[10]->setNbGraines(0);
	pf.m_cases[11]->setNbGraines(0);
	QTest::newRow("0:0 1:10 6:2 7:1 8:2 9:1 10:2 11:1") << p << 15 << 1 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(2);
	p.m_cases[1]->setNbGraines(1);
	p.m_cases[2]->setNbGraines(2);
	p.m_cases[3]->setNbGraines(1);
	p.m_cases[4]->setNbGraines(2);
	p.m_cases[5]->setNbGraines(1);
	p.m_cases[6]->setNbGraines(0);
	p.m_cases[7]->setNbGraines(10);
	pf.m_cases[0]->setNbGraines(0);
	pf.m_cases[1]->setNbGraines(0);
	pf.m_cases[2]->setNbGraines(0);
	pf.m_cases[3]->setNbGraines(0);
	pf.m_cases[4]->setNbGraines(0);
	pf.m_cases[5]->setNbGraines(0);
	pf.m_cases[6]->setNbGraines(0);
	pf.m_cases[7]->setNbGraines(0);
	pf.m_cases[8]->setNbGraines(5);
	pf.m_cases[9]->setNbGraines(5);
	pf.m_cases[10]->setNbGraines(5);
	pf.m_cases[11]->setNbGraines(5);
	QTest::newRow("0:2 1:1 2:2 3:1 4:2 5:1 6:0 7:10") << p << 15 << 7 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(1);
	p.m_cases[1]->setNbGraines(1);
	p.m_cases[4]->setNbGraines(8);
	p.m_cases[6]->setNbGraines(1);
	p.m_cases[7]->setNbGraines(1);
	p.m_cases[8]->setNbGraines(1);
	p.m_cases[9]->setNbGraines(1);
	p.m_cases[10]->setNbGraines(1);
	p.m_cases[11]->setNbGraines(1);
	pf.m_cases[0]->setNbGraines(2);
	pf.m_cases[1]->setNbGraines(1);
	pf.m_cases[4]->setNbGraines(0);
	pf.m_cases[5]->setNbGraines(5);
	pf.m_cases[6]->setNbGraines(2);
	pf.m_cases[7]->setNbGraines(2);
	pf.m_cases[8]->setNbGraines(2);
	pf.m_cases[9]->setNbGraines(2);
	pf.m_cases[10]->setNbGraines(2);
	pf.m_cases[11]->setNbGraines(2);
	QTest::newRow("0:1 1:1 4:8 6:1 7:1 8:1 9:1 10:1 11:1") << p << 0 << 4 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(1);
	p.m_cases[1]->setNbGraines(1);
	p.m_cases[2]->setNbGraines(1);
	p.m_cases[3]->setNbGraines(1);
	p.m_cases[4]->setNbGraines(1);
	p.m_cases[5]->setNbGraines(1);
	p.m_cases[7]->setNbGraines(1);
	p.m_cases[6]->setNbGraines(1);
	p.m_cases[10]->setNbGraines(8);
	pf.m_cases[0]->setNbGraines(2);
	pf.m_cases[1]->setNbGraines(2);
	pf.m_cases[2]->setNbGraines(2);
	pf.m_cases[3]->setNbGraines(2);
	pf.m_cases[4]->setNbGraines(2);
	pf.m_cases[5]->setNbGraines(2);
	pf.m_cases[6]->setNbGraines(2);
	pf.m_cases[7]->setNbGraines(1);
	pf.m_cases[10]->setNbGraines(0);
	pf.m_cases[11]->setNbGraines(5);
	QTest::newRow("0:1 1:1 2:1 3:1 4:1 5:1 6:1 7:1 10:8") << p << 0 << 10 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(1);
	p.m_cases[1]->setNbGraines(1);
	p.m_cases[4]->setNbGraines(5);
	p.m_cases[6]->setNbGraines(1);
	p.m_cases[7]->setNbGraines(1);
	p.m_cases[8]->setNbGraines(1);
	p.m_cases[9]->setNbGraines(1);
	p.m_cases[10]->setNbGraines(1);
	p.m_cases[11]->setNbGraines(1);
	pf.m_cases[0]->setNbGraines(1);
	pf.m_cases[1]->setNbGraines(1);
	pf.m_cases[4]->setNbGraines(0);
	pf.m_cases[5]->setNbGraines(5);
	pf.m_cases[6]->setNbGraines(0);
	pf.m_cases[7]->setNbGraines(0);
	pf.m_cases[8]->setNbGraines(0);
	pf.m_cases[9]->setNbGraines(0);
	pf.m_cases[10]->setNbGraines(1);
	pf.m_cases[11]->setNbGraines(1);
	QTest::newRow("0:1 1:1 4:5 6:1 7:1 8:1 9:1 10:1 11:1") << p << 8 << 4 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[0]->setNbGraines(1);
	p.m_cases[1]->setNbGraines(1);
	p.m_cases[2]->setNbGraines(1);
	p.m_cases[3]->setNbGraines(1);
	p.m_cases[4]->setNbGraines(1);
	p.m_cases[5]->setNbGraines(1);
	p.m_cases[7]->setNbGraines(1);
	p.m_cases[6]->setNbGraines(1);
	p.m_cases[10]->setNbGraines(5);
	pf.m_cases[0]->setNbGraines(0);
	pf.m_cases[1]->setNbGraines(0);
	pf.m_cases[2]->setNbGraines(0);
	pf.m_cases[3]->setNbGraines(0);
	pf.m_cases[4]->setNbGraines(1);
	pf.m_cases[5]->setNbGraines(1);
	pf.m_cases[6]->setNbGraines(1);
	pf.m_cases[7]->setNbGraines(1);
	pf.m_cases[10]->setNbGraines(0);
	pf.m_cases[11]->setNbGraines(5);
	QTest::newRow("0:1 1:1 2:1 3:1 4:1 5:1 6:1 7:1 10:5") << p << 8 << 10 << pf;

	p = Plateau();
	pf = Plateau();
	pf.m_cases[0]->setNbGraines(0);
	pf.m_cases[1]->setNbGraines(5);
	pf.m_cases[2]->setNbGraines(5);
	pf.m_cases[3]->setNbGraines(5);
	pf.m_cases[4]->setNbGraines(5);
	QTest::newRow("")  << p << 0 << 0 << pf;

	p = Plateau();
	pf = Plateau();
	pf.m_cases[6]->setNbGraines(0);
	pf.m_cases[7]->setNbGraines(5);
	pf.m_cases[8]->setNbGraines(5);
	pf.m_cases[9]->setNbGraines(5);
	pf.m_cases[10]->setNbGraines(5);
	QTest::newRow("")  << p << 0 << 6 << pf;

	p = Plateau();
	pf = Plateau();
	pf.m_cases[4]->setNbGraines(0);
	pf.m_cases[5]->setNbGraines(5);
	pf.m_cases[6]->setNbGraines(5);
	pf.m_cases[7]->setNbGraines(5);
	pf.m_cases[8]->setNbGraines(5);
	QTest::newRow("")  << p << 0 << 4 << pf;

	p = Plateau();
	pf = Plateau();
	pf.m_cases[10]->setNbGraines(0);
	pf.m_cases[11]->setNbGraines(5);
	pf.m_cases[0]->setNbGraines(5);
	pf.m_cases[1]->setNbGraines(5);
	pf.m_cases[2]->setNbGraines(5);
	QTest::newRow("")  << p << 0 << 10 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[3]->setNbGraines(20);
	pf.m_cases[0]->setNbGraines(6);
	pf.m_cases[1]->setNbGraines(5);
	pf.m_cases[2]->setNbGraines(5);
	pf.m_cases[3]->setNbGraines(0);
	pf.m_cases[4]->setNbGraines(6);
	pf.m_cases[5]->setNbGraines(6);
	pf.m_cases[6]->setNbGraines(6);
	pf.m_cases[7]->setNbGraines(6);
	pf.m_cases[8]->setNbGraines(6);
	pf.m_cases[9]->setNbGraines(6);
	pf.m_cases[10]->setNbGraines(6);
	pf.m_cases[11]->setNbGraines(6);
	QTest::newRow("")  << p << 0 << 3 << pf;

	p = Plateau();
	pf = Plateau();
	p.m_cases[9]->setNbGraines(20);
	pf.m_cases[0]->setNbGraines(6);
	pf.m_cases[1]->setNbGraines(6);
	pf.m_cases[2]->setNbGraines(6);
	pf.m_cases[3]->setNbGraines(6);
	pf.m_cases[4]->setNbGraines(6);
	pf.m_cases[5]->setNbGraines(6);
	pf.m_cases[6]->setNbGraines(6);
	pf.m_cases[7]->setNbGraines(5);
	pf.m_cases[8]->setNbGraines(5);
	pf.m_cases[9]->setNbGraines(0);
	pf.m_cases[10]->setNbGraines(6);
	pf.m_cases[11]->setNbGraines(6);
	QTest::newRow("")  << p << 0 << 9 << pf;
}

QTEST_APPLESS_MAIN(TestsJeu);

#include "tst_testsjeu.moc"
